<?php

namespace app\model;

use app\controller\wmapi\IndexController;
use support\Db;
use support\Model;

class WaMenu
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_menu';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * 菜单列表 - 分页
     * @return false|string
     */
    public static function list($page,$pageNum,$fields,$condition)
    {

        $offset = ($page - 1) * $pageNum;
        $model = Db::table('wa_menu')
            ->select($fields)
            ->where($condition);
        $count = $model->where($condition)->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
//        ->paginate($pageNum);

        return json_encode(['list' => $list, 'count' => $count]);
    }


    /**
     * 菜单列表 - 无分页
     * @param $page
     * @param $pageNum
     * @param $fields
     * @param $condition
     * @return false|string
     */
    public static function all($fields,$condition)
    {
        $list = Db::table('wa_menu')
            ->select($fields)
            ->where($condition)
            ->orderBy('id', 'asc')
            ->get();
        if ($list){
            // 0 级 父级模块
            foreach ($list as $k){
                if ($k->meta){
                    $k->meta = json_decode($k->meta);
                }
                // 1 级 菜单列表模块
                $children = Db::table('wa_menu')->where(['pid' => intval($k->id),'hidden' => 'false'])->orderBy('id','asc')->get();
                $children = json_decode($children);
                if ($children){
                    $k->children = $children;
                    // 1 级 菜单列表模块
                    foreach ($k->children as $ke){

                        $ke->meta = json_decode($ke->meta);
                        // 2 级 子菜单列表模块
                        $children_second = Db::table('wa_menu')->where(['pid' => $ke->id,'type' => 1,'hidden' => 'false'])->get();
//                        $children_second = Db::table('wa_menu')->where(['pid' => intval($ke->id)])->get();
                        $children_second = json_decode($children_second);
                        if ($children_second){
                            $ke->children = $children_second;
                            // 2 级 子菜单列表模块
                            foreach ($ke->children as $kes){
//                                $kes->meta = get_object_vars(json_decode($kes->meta));

                                $kes_meta = json_decode($kes->meta);
                                if ($kes_meta){
                                    $kes->meta = get_object_vars(json_decode($kes->meta));
                                }else{
                                    $kes->meta = json_decode($kes->meta);
                                }
                                // 3 级 子子级菜单列表模块
                                $menu_btn_third = Db::table('wa_menu')->where(['pid' => $kes->id,'type' => 2])->get();
//                        $children_second = Db::table('wa_menu')->where(['pid' => intval($ke->id)])->get();
                                $menu_btn_third = json_decode($menu_btn_third);
                                if ($menu_btn_third){
                                // 3 级 子子级菜单列表模块
                                foreach ($menu_btn_third as $third){
//                                    $third->meta = get_object_vars(json_decode($third->meta));
                                    $third_meta = json_decode($third->meta);
                                    if ($third_meta){
                                        $third->meta = get_object_vars(json_decode($third->meta));

                                    }else{
                                        $third->meta = json_decode($third->meta);
                                    }

                                }
                                    $kes->menu_btn = $menu_btn_third;
                                }
                            }


                        }

                        // 2 级 子菜单按钮模块
                        $menu_btn = Db::table('wa_menu')->where(['pid' => $ke->id,'type' => 2,'hidden' => 'false'])->get();
                        $menu_btn = json_decode($menu_btn);
                        if ($menu_btn){
                            // 2 级 子菜单按钮模块
                            foreach ($menu_btn as $keme){
//                                $keme->meta = get_object_vars(json_decode($keme->meta));
                                $keme_meta = json_decode($keme->meta);
                                if ($keme_meta){
                                    $keme->meta = get_object_vars(json_decode($keme->meta));

                                }else{
                                    $keme->meta = json_decode($keme->meta);
                                }
                            }
                        }
                        $ke->menu_btn = $menu_btn;


                    }
                }
            }
        }
        return json_encode($list);
    }




    public static function find($condition)
    {
        $res = Db::table('wa_menu')
            ->where($condition)
            ->first();
        return json_encode($res);
    }

    public static function created($data){

        return Db::table('wa_menu')->insertGetId($data);

    }

    public static function updated($condition,$data){

        $res = Db::table('wa_menu')->where($condition)->update($data);
        return json_encode($res);

    }

    public static function deleted($condition){

        $res = Db::table('wa_menu')->where($condition)->delete();
        return json_encode($res);

    }



}